package cn.iune.controllers;

import java.io.FileWriter;
import java.io.IOException;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;

@RestController
public class ThymeleafController {

	@GetMapping("/thymeleaf/{msg}")
    public void learn(@PathVariable String msg) throws IOException {
        //创建模版加载器
        ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver();
        resolver.setPrefix("templates/");  //模板文件的所在目录
        resolver.setSuffix(".html");       //模板文件后缀
        //创建模板引擎
        TemplateEngine templateEngine = new TemplateEngine();
        //将加载器放入模板引擎
        templateEngine.setTemplateResolver(resolver);
        //创建字符输出流并且自定义输出文件的位置和文件名
        FileWriter writer = new FileWriter("F:/thymeleafTest/index.html");
        //创建Context对象(存放Model)
        Context context = new Context();
        //放入数据
        context.setVariable("hello",msg);
        //创建静态文件,"text"是模板html名字
        templateEngine.process("index",context,writer);
    }
	
}
